package com.niodata.dp.web;

import com.niodata.dp.core.ConfigurationKeys;
import com.niodata.dp.core.DpConfiguration;
import com.niodata.dp.core.context.ContextProvider;
import com.niodata.dp.mybatis.MyBatisConfiguration;
import com.niodata.dp.web.security.ContextUtil;
import com.niodata.dp.web.security.DpJdbcUserDetailService;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.annotation.PropertySources;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.scheduling.TaskScheduler;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.socket.config.annotation.EnableWebSocket;

@SuppressWarnings("deprecation")
@EnableAutoConfiguration(exclude = {MongoAutoConfiguration.class})
@EnableRedisHttpSession
@Configuration
@ComponentScan({"com.niodata.dp.*", "com.niodata.dt.*"})
@SpringBootApplication
@Import({MyBatisConfiguration.class, SpringFoxConfig.class})
@MapperScan({"com.niodata.dp", "com.niodata.dt"})
@EnableScheduling
@EnableWebSocket
//@EnableCasClient
@PropertySources(
      value = {
            @PropertySource("classpath:application.properties"),
            @PropertySource("classpath:hdfs.properties"),
            @PropertySource("classpath:datasource.properties"),
            @PropertySource("classpath:hive.properties"),
            @PropertySource("classpath:kdcldap.properties"),
            @PropertySource("classpath:monitor.properties"),
            @PropertySource("classpath:quartz.properties"),
            @PropertySource("classpath:redis.properties"),
            @PropertySource("classpath:workflow.properties"),
            @PropertySource("classpath:plugin.properties"),
            @PropertySource("classpath:zeppelin.properties")
      }
)
public class NdpWebApp {

  @Autowired
  @Qualifier("dpUserDetailsService")
  private DpJdbcUserDetailService jdbcUserDetailService;

  public static void main(String[] args) {
    SpringApplication.run(NdpWebApp.class, args);
    init();
  }

  private static void init() {
    ContextProvider.setFactory(ContextUtil::currentContext);
    ApplicationContext context = ApplicationContextHolder.getApplicationContext();
  }



  @Bean
  public JedisConnectionFactory connectionFactory() {
    JedisConnectionFactory factory = new JedisConnectionFactory();
    factory.setHostName(DpConfiguration.getString(ConfigurationKeys.REDIS_IP));
    factory.setPort(DpConfiguration.getInt(ConfigurationKeys.REDIS_PORT));
    if (DpConfiguration.getString(ConfigurationKeys.REDIS_PASSWORD, null) != null) {
      factory.setPassword(DpConfiguration.getString(ConfigurationKeys.REDIS_PASSWORD));
    }
    return factory;
  }

  @Configuration
  public class ScheduledConfig {

    @Bean
    public TaskScheduler taskScheduler() {
      ThreadPoolTaskScheduler scheduling = new ThreadPoolTaskScheduler();
      scheduling.setPoolSize(10);
      scheduling.initialize();
      return scheduling;

    }
  }

}

